#include "reg.h"
#include <string.h>

int tok_register(sqlite3 *db, const char *zName,
		const sqlite3_tokenizer_module *p) {
	int rc;
	sqlite3_stmt *pStmt;
	const char *zSql = "SELECT fts3_tokenizer(?, ?)";

	rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
	if (rc != SQLITE_OK) {
		return rc;
	}

	sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
	sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
	sqlite3_step(pStmt);

	return sqlite3_finalize(pStmt);
}

int tok_registered(sqlite3 *db, const char *zName,
		const sqlite3_tokenizer_module **pp) {
	int rc;
	sqlite3_stmt *pStmt;
	const char *zSql = "SELECT fts3_tokenizer(?)";

	*pp = 0;
	rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
	if (rc != SQLITE_OK) {
		return rc;
	}
	void *a = 0;
	sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
	if (SQLITE_ROW == sqlite3_step(pStmt)) {
		if (sqlite3_column_type(pStmt, 0) == SQLITE_BLOB) {
			memcpy(pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
		}
	}

	return sqlite3_finalize(pStmt);
}
